/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

import java.awt.Button;
import java.awt.Font;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class infoPanel extends JPanel implements ActionListener {
    //GameEntityFacadeRemote GameSession;
    JLabel playerLabel;
    
    JPanel infoPane;
    JPanel playerPane;
    JPanel dicePane;
    Button rollButton;
    
    boardPanel parent; 
    Board board;
    GameState gameState;

    public infoPanel() {
      
    }
    
    public void initialize() {
      parent = (boardPanel) this.getParent();
      board = parent.game.board;
      gameState = parent.game.gameState;
      
      BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
      this.setLayout(layout);
      this.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
      
      rollButton = new Button("Roll Dice");
      rollButton.addActionListener(this);
      
      addInfoPane();
      addPlayerPane();
      add(rollButton);
      addDicePane();
    }
    
    public void addInfoPane(){
      infoPane = new JPanel();    
  	  infoPane.setBorder(BorderFactory.createTitledBorder("Game Info"));
      BoxLayout layout = new BoxLayout(infoPane, BoxLayout.Y_AXIS);
      infoPane.setLayout(layout);
      
      JLabel currentLabel = new JLabel("Current Player:");
      currentLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
      
      int currentIndex = gameState.getCurrentPlayer();
      String playerName = "";
      if(board.getPlayer().length > 0) {
        playerName = board.getPlayer()[currentIndex].getName();
      }
      playerLabel = new JLabel(playerName);
      playerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
      
      JLabel totalPlayerLabel = new JLabel("Total # Players: " + board.getPlayer().length);
      totalPlayerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

      infoPane.add(currentLabel);
      infoPane.add(playerLabel);
      infoPane.add(totalPlayerLabel);
      
      this.add(infoPane);
    }
    
    public void addPlayerPane(){
      playerPane = new JPanel();    
  	  playerPane.setBorder(BorderFactory.createTitledBorder("Player Info"));
      BoxLayout layout = new BoxLayout(playerPane, BoxLayout.Y_AXIS);
      playerPane.setLayout(layout);
      
      updatePlayers();
      
      playerPane.setMinimumSize(new Dimension(500, 100));
      
      this.add(playerPane);
    }
    
    public void addDicePane() {
      Dice[] diceList = board.getDice();
      dicePane = new JPanel();
      dicePane.setBorder(BorderFactory.createTitledBorder("Dice"));
      dicePane.setLayout(new GridLayout((int)Math.ceil((double)diceList.length/(double)2), 2));
      
      dicePane.setMaximumSize(new Dimension(175, 500));
      this.add(dicePane);
      
      updateDice();
    }

    public void updateDice() {
      dicePane.removeAll();
      
      Dice[] diceList = board.getDice();
      for(int i=0; i<diceList.length; i++)
      {
        dicePane.add(new Button("" + diceList[i].getNumber()));
      }
      
      dicePane.validate();
      dicePane.repaint();
    }
    
    public void updateInfo() {
      int currentIndex = gameState.getCurrentPlayer();
      playerLabel.setText(board.getPlayer()[currentIndex].getName());
    }
    
    public void updatePlayers() {
      playerPane.removeAll();
      boolean[] active = gameState.getActivePlayer();
      Player[] players = board.getPlayer();
      JLabel label, pointsLabel, delayLabel;
      Font bold = new Font(null, Font.BOLD, 14);
      for(int i=0; i < active.length; i++) {
        if(!active[i]) continue;
        label = new JLabel(players[i].getName() + ": ");
        pointsLabel = new JLabel("points: " + players[i].getPoints());
        delayLabel = new JLabel("delay: " + players[i].getDelay());
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
        pointsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        delayLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        
        label.setFont(bold);
        
        playerPane.add(label);
        playerPane.add(pointsLabel);
        playerPane.add(delayLabel);
      }
      
      playerPane.validate();
      playerPane.repaint();
    }
    
    private void rollDice() {
      Dice[] diceList = board.getDice();
      for(int i=0; i<diceList.length; i++) {
        diceList[i].roll();
      }

      updateDice();
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
      // only action here is roll button
      if(gameState.isRunning()) {
    	  if(board.getPlayer().length > 0) {
    		  rollDice();
    	      parent.game.execTurn();
    	  }
      }
      else {
        JOptionPane.showMessageDialog(null, "Game has already ended", "Game Over", JOptionPane.INFORMATION_MESSAGE);
      }
    }
 }
